package common.server.utils;

import java.io.IOException;
import java.net.Inet4Address;
import java.net.InetAddress;
import java.net.NetworkInterface;
import java.net.UnknownHostException;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

/**
 * @author Administrator
 * @version [版本号, 2018/1/5 0005]
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class IPHelper {



    public static void main(String[] args) {
        System.out.println(findFirstNonLoopbackAddress());

    }

    /**
     * 查找IP地址
     *
     * @return
     */
    public static InetAddress findFirstNonLoopbackAddress() {
        InetAddress result = null;

        try {
            int lowest = 2147483647;
            Enumeration nics = NetworkInterface.getNetworkInterfaces();
            label61: while (true) {
                Enumeration<InetAddress> addrs;
                NetworkInterface ifc;
                do {
                    while (true) {
                        do {
                            if (!nics.hasMoreElements()) {
                                break label61;
                            }
                            ifc = (NetworkInterface) nics.nextElement();
                        } while (!ifc.isUp());
                        System.out.println(
                                "Testing interface: " + ifc.getDisplayName());
                        if (ifc.getIndex() >= lowest && result != null) {
                            if (result != null) {
                                continue;
                            }
                            break;
                        }

                        lowest = ifc.getIndex();
                        addrs =  ifc.getInetAddresses();
                        if (null != addrs) {
                            while (addrs.hasMoreElements()) {
                                InetAddress address = addrs.nextElement();
                                if (address instanceof Inet4Address
                                        && !address.isLoopbackAddress()
                                        && !ignoreAddress(address)) {
                                    result = address;
                                }
                            }
                        }
                        break;
                    }
                } while (true);
            }
        } catch (IOException var8) {
            var8.printStackTrace();
        }

        if (result != null) {
            return result;
        } else {
            try {
                return InetAddress.getLocalHost();
            } catch (UnknownHostException var7) {
                var7.printStackTrace();
                return null;
            }
        }
    }

    static boolean ignoreAddress(InetAddress address) {
        return true;
    }

    static boolean ignoreInterface(String interfaceName) {
        Map var1 = new HashMap<String, String>();
        Iterator var2 = var1.keySet().iterator();

        String regex;
        do {
            if (!var2.hasNext()) {
                return false;
            }

            regex = (String) var2.next();
        } while (!interfaceName.matches(regex));
        return true;
    }
}
